package com.kang.controller;


import com.kang.aop.operLog.OperLog;
import com.kang.entity.SysUser;
import com.kang.menu.BusinessType;
import com.kang.service.SysUserService;
import com.kang.util.WebResponse;
import com.kang.vo.AssginRoleVo;
import com.kang.vo.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yuhongkang
 * @since 2023-01-04
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("用户列表")
    @GetMapping("/{page}/{limit}")
    public WebResponse list(@PathVariable Long page, @PathVariable Long limit, SysUserQueryVo sysUserQueryVo) {
        try {
            return WebResponse.resSuccess("用户列表查询成功",sysUserService.selectSysUserList(page,limit,sysUserQueryVo));
        } catch (Exception ex) {
            log.info("用户列表失败",ex);
            return WebResponse.resFail("用户列表失败",null);
        }
    }

    @OperLog(title = "添加用户", businessType = BusinessType.INSERT)
    @ApiOperation("添加用户")
    @PostMapping("saveSysUser")
    public WebResponse saveSysUser(@RequestBody SysUser sysUser) {
        try {
            return WebResponse.resSuccess("添加用户成功",sysUserService.savsSysUser(sysUser));
        } catch (Exception ex) {
            log.info("添加用户失败",ex);
            return WebResponse.resFail("添加用户失败",null);
        }
    }

    @ApiOperation("根据ID查询的接口")
    @GetMapping("getUserById/{id}")
    public WebResponse getUserById(@PathVariable Integer id) {
        try {
            return WebResponse.resSuccess("根据ID查询的接口成功",sysUserService.getUserById(id));
        } catch (Exception ex) {
            log.info("根据ID查询的接口失败",ex);
            return WebResponse.resFail("根据ID查询的接口失败",null);
        }
    }

    @OperLog(title = "修改用户", businessType = BusinessType.UPDATE)
    @ApiOperation("修改用户")
    @PostMapping("updateSysUser")
    public WebResponse updateSysUser(@RequestBody SysUser sysUser) {
        try {
            return WebResponse.resSuccess("修改用户成功",sysUserService.updateSysUser(sysUser));
        } catch (Exception ex) {
            log.info("修改用户失败",ex);
            return WebResponse.resFail("修改用户失败",null);
        }
    }

    @OperLog(title = "逻辑删除角色", businessType = BusinessType.DELETE)
    @ApiOperation("删除用户")
    @DeleteMapping("deleteSysUser/{id}")
    public WebResponse deleteSysUserById(@PathVariable Integer id) {
        try {
            return WebResponse.resSuccess("删除用户成功",sysUserService.deleteSysUserById(id));
        } catch (Exception ex) {
            log.info("删除用户失败",ex);
            return WebResponse.resFail("删除用户失败",null);
        }
    }

    @OperLog(title = "更改用户状态接口", businessType = BusinessType.UPDATE)
    @ApiOperation("更改用户状态接口")
    @GetMapping("updateStatus/{id}/{status}")
    public WebResponse updateStatus(@PathVariable Integer id,@PathVariable Integer status) {
        try {
            return WebResponse.resSuccess("更改用户状态接口成功",sysUserService.updateStatus(id,status));
        } catch (Exception ex) {
            log.info("更改用户状态接口成功失败",ex);
            return WebResponse.resFail("更改用户状态接口成功失败",null);
        }
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public WebResponse toAssign(@PathVariable Integer userId) {
        try {
            Map<String,Object> resultMap = sysUserService.getRoleId(userId);
            return WebResponse.resSuccess("根据用户获取角色数据成功",resultMap);
        } catch (Exception ex) {
            log.info("更改用户状态接口成功失败",ex);
            return WebResponse.resFail("更改用户状态接口成功失败",null);
        }
    }

    @OperLog(title = "根据用户分配角色", businessType = BusinessType.OTHER)
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public WebResponse doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        try {
            return WebResponse.resSuccess("根据用户分配角色成功",sysUserService.doAssign(assginRoleVo));
        } catch (Exception ex) {
            log.info("根据用户分配角色失败",ex);
            return WebResponse.resFail("根据用户分配角色失败",null);
        }
    }


}

